package me.luger.project.picc.controller.admin;

import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.dao.nutz.entity.page.DataTableResult;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import me.luger.project.picc.service.PrizeListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import me.luger.project.picc.service.PiccMeetingService;
import me.luger.project.picc.entity.PiccMeeting;

import java.util.List;

/**
 * 我和保单有个约会 controller
 */
@Controller
@RequestMapping("/private/picc/piccMeeting")
public class PiccMeetingController extends BaseController {

    private String viewBasePath = "/admin/modules/piccMeeting/";

    @Autowired
    private PiccMeetingService piccMeetingService;

    @Autowired
    private PrizeListService prizeListService;

    @RequestMapping(value = "",name = "我和保单有个约会列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取我和保单有个约会列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        DataTableResult dataTableResult = piccMeetingService.data(dataTableRequest);
        List<PiccMeeting> piccMeetingList = dataTableResult.getData();

        for(PiccMeeting piccMeeting : piccMeetingList){
            if(piccMeeting.getDoPrizeStatus() == 1 && prizeListService.isGetPrizeByPid(piccMeeting.getPid())){//该保单中过奖
                piccMeeting.setDoPrizeStatus(0);
            }

        }
        return piccMeetingService.data(dataTableRequest);
    }



    @RequestMapping(value = "delete",name = "删除我和保单有个约会接口")
    @ResponseBody
    public Object delete(String[] ids){
        piccMeetingService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取我和保单有个约会详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        PiccMeeting piccMeeting = piccMeetingService.fetchById(id);
        setAttr("piccMeeting",piccMeeting);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加我和保单有个约会页面")
    public ModelAndView add(){
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加我和保单有个约会接口")
    @ResponseBody
    @OpLog(name = "添加我和保单有个约会",msg = "添加我和保单有个约会")
    public Object adddo(PiccMeeting piccMeeting){
        try{
            piccMeeting = piccMeetingService.insert(piccMeeting);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新我和保单有个约会信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        PiccMeeting piccMeeting = piccMeetingService.fetchById(id);
        setAttr("piccMeeting",piccMeeting);
        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新我和保单有个约会接口")
    @ResponseBody
    public Object editDo(PiccMeeting piccMeeting){
        try{
            piccMeetingService.updateIgnoreNull(piccMeeting);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
